<?php

class Town extends CI_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->permit_library->check_permit();
	  	$this->session->set_userdata(array('Url'=>uri_string()));   
		$this->load->model('town_model','town');
	}

	function index()
	{
		$data = array();
		$data['title'] = "Danh sách tỉnh thành";
		$data['add'] = 'town/add';
		
		$start = (int)$this->uri->segment(4) ;
		$config['base_url'] = base_url().'town/index/'.$start;
		$data['num'] = $config['total_rows'] =  $this->town->get_num();
		$config['per_page']  =  40 ;
		$config['uri_segment'] = 4; 
		$this->pagination->initialize($config);
		$data['pagination']    = $this->pagination->create_links();
		
		$data['list'] =   $this->town->get_all($config['per_page'], $start);
		$data['page'] = 'town/list' ;
		$this->load->view('layout/skin', $data);
		$this->createTown();
		$this->createNational();
	}

	function add()
	{
		$data = array();
		$data['title'] = lang('add_news');
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('cd_city',lang('code'),'trim|required');
			$this->form_validation->set_rules('town_name',lang('name_city'),'trim|required');
			if($this->form_validation->run()== FALSE){
			  $this->pre_message = validation_errors();
			}else{
				$data =  $this-> build_data($_POST,$flag=1); 
				if($this->town->save_city($data)){
									 
				  $this->session->set_flashdata('message',lang('successful'));
				  redirect('town');
			  }
			}
			$data['message'] = $this->pre_message;
		}
		
		$data['page'] = 'town/add' ;
		$this->load->view('layout/skin', $data);
	}  

	function edit($id)
	{
		if(isset($_POST['bt_submit']))
		{
			$this->form_validation->set_rules('town_name',lang('name_city'),'trim|required');
			if($this->form_validation->run()== FALSE){
				$this->pre_message = validation_errors();
			}else{
				$data =  $this-> build_data(); 
				if($this->town->save($data)){
					$this->session->set_flashdata('message',lang('successful'));
					redirect('town');
				}
			}
		}
		
		$data['title'] = "Cập nhật tỉnh thành";
		$data['rs'] = $this->town->get($id);
		$data['page'] = 'town/edit' ;
		$this->load->view('layout/skin', $data);
	}  

	function build_data($flag=0)
	{
		$req["town_name"]= $this->input->post("town_name");
		$req["sort_no"]= (int) $this->input->post("sort_no");
		$req["bl_active"]= (int) $this->input->post("bl_active");
		return $req;
	}

	function del()
	{
	
		$id = $this->uri->segment(3);
		$page = $this->uri->segment(4);
		if($this->town->del($id))
			$this->session->set_flashdata('message',lang('delete_success'));
		else 
			$this->session->set_flashdata('message',lang('delete_unsuccess'));
		redirect('town');
  	}

	function dels()
	{
	
		if(!empty($_POST['ar_id']))
		{
			$page = (int)$this->input->post('page');
			$ar_id = $this->input->post('ar_id');
					
			if(!empty($_POST['btn_submit']))
			{
				for($i = 0; $i < sizeof($ar_id); $i ++) 
				{
					if ($ar_id[$i]){
						if($this->town->del($ar_id[$i]))
							$this->session->set_flashdata('message',lang('delete_success'));
						else $this->session->set_flashdata('message',lang('delete_unsuccess'));
					}
				}
			}
		}
		redirect('town');
	}  
	
	function createTown(){
		 $data=$this->town->get_country();
		 $str = "<?php  \n/**\n* Cache file for news.dev \n* Date: ".date('d/m/y H:i:s').".\n**/\n";
		 $str.="\$town_total=".count($data).";\n";
		 foreach($data as $v){
		 $str.="\$town['$v->town_id']='$v->town_name';\n";
		 }
		file_put_contents(DIR_DATA.'/town.php',$str);
	}
	function createNational(){
		 $data=$this->town->get_national();
		 $str = "<?php  \n/**\n* Cache file for news.dev \n* Date: ".date('d/m/y H:i:s').".\n**/\n";
		 $str.="\$national_total=".count($data).";\n";
		 foreach($data as $v){
		 $str.="\$national['$v->countryid']=\"$v->name\";\n";
		 }
		file_put_contents(DIR_DATA.'/national.php',$str);
	}
  
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */